package com.party.service.news.impl;

import com.party.common.Page;
import com.party.mapper.NewsMapper;
import com.party.model.ExamExample;
import com.party.model.News;
import com.party.model.NewsExample;
import com.party.service.news.NewsService;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: beyond
 * Date: 14-9-18
 * Time: 下午10:38
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class NewsServiceImpl implements NewsService {

    @Resource
    NewsMapper newsMapper;

    @Override
    public int addNews(News news) {
        int num = newsMapper.insertSelective(news);
        return num;
    }

    @Override
    public int deleteNewsByPrimaryKey(Integer id) {
        int num = newsMapper.deleteByPrimaryKey(id);
        return num;
    }

    @Override
    public int deleteExamByIds(String ids) {
        int num = 0;
        if (ids != null&&!"".equals(ids)) {
            String roids[] = ids.split(",");
            for (String roid : roids) {
                num += newsMapper.deleteByPrimaryKey(Integer.parseInt(roid));
            }
        }
        return num;
    }

    @Override
    public News getNewsByPrimaryKey(Integer id) {
        News news = newsMapper.selectByPrimaryKey(id);
        return news;
    }

    @Override
    public int updateNewsByPrimaryKey(News news) {
        int num = newsMapper.updateByPrimaryKeySelective(news);
        return num;
    }

    @Override
    public List<News> getNewsList(String type,Integer newsNum) {
        NewsExample newsExample = new NewsExample();
        NewsExample.Criteria criteria = newsExample.createCriteria();
        News news=new News();
        //获取指定条数的数据
//        news.setType(type);
//        news.setNewsNum(newsNum);
        criteria.andTypeEqualTo(type);
        newsExample.setOrderByClause("time desc");
        //获取指定的页码数据
        newsExample.setBeginNum(0);
        newsExample.setEndNum(newsNum);
        List<News> newsList = newsMapper.getListPage(newsExample);
//        List<News> newsList = newsMapper.getListPage(news);
        return newsList;
    }

    @Override
    public Page getNewsList(News news, int pageSize, int pageNumber) {
        NewsExample newsExample = new NewsExample();
        /**
         * 可以添加查询条件
         */
        if(news != null)
        {
            NewsExample.Criteria criteria = newsExample.createCriteria();
            if(news.getTitle() != null && !"".equals(news.getTitle()))
            {
                criteria.andTitleLike("%"+news.getTitle()+"%");
            }
            if(news.getType() != null&& !"".equals(news.getType()))
            {
                criteria.andTypeEqualTo(news.getType());
            }
        }
        newsExample.setOrderByClause("time desc");

        //获取总的记录数量
        int count = newsMapper.countByExample(newsExample);
        //获取指定的页码数据
        newsExample.setBeginNum((pageNumber - 1) * pageSize);
        newsExample.setEndNum(pageSize);
        List<News> list = newsMapper.getListPage(newsExample);
        //组合返回page
        Page page = new Page(pageNumber, count, pageSize, list);

        return page;
    }
}
